package cn.legend.travel.admin.content.service.impl;

import cn.legend.travel.admin.content.dao.persist.repository.IGuideDetailRepository;
import cn.legend.travel.admin.content.dao.persist.repository.IGuideRepository;
import cn.legend.travel.admin.content.pojo.entity.Guide;
import cn.legend.travel.admin.content.pojo.param.GuideIssueParam;
import cn.legend.travel.admin.content.pojo.vo.GuideCheckVO;
import cn.legend.travel.admin.content.pojo.vo.GuideDetailVO;
import cn.legend.travel.admin.content.service.IGuideService;
import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.vo.PageData;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class GuideServiceImpl implements IGuideService {
    private final IGuideRepository guideRepository;
    private final IGuideDetailRepository guideDetailRepository;
    @Value("${travel.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Override
    public PageData<GuideCheckVO> list(Integer pageNum) {
        return guideRepository.list(pageNum, defaultQueryPageSize);
    }

    @Override
    public void check(Long id, Integer state) {
        int row = guideRepository.check(id, state);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "服务器忙,请稍后再试");
        }
    }

    @Override
    public void changeDisplay(Long id, Integer isDisplay) {
        int row = guideRepository.changeDisplay(id, isDisplay);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "服务器忙,请稍后再试");
        }
    }

    @Override
    public GuideDetailVO getDetail(Long id) {
        return guideRepository.getDetail(id);
    }

    @Override
    @Transactional
    public void issue(GuideIssueParam param) {
        Long id = guideRepository.insertBasic(param);
        int row = guideDetailRepository.insertDetail(id, param.getContent());
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "服务器忙,请稍后再试");
        }
    }
}
